package org.jeecg.modules.basic.check.service.impl;

import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.basic.check.entity.LwgyCheckEquipment;
import org.jeecg.modules.basic.check.entity.LwgyCheckRecords;
import org.jeecg.modules.basic.check.mapper.LwgyCheckRecordsMapper;
import org.jeecg.modules.basic.check.mapper.LwgyCheckEquipmentMapper;
import org.jeecg.modules.basic.check.service.ILwgyCheckEquipmentService;
import org.jeecg.modules.basic.repair.entity.LwgyRepairMaintenance;
import org.jeecg.modules.basic.repair.service.ILwgyRepairMaintenanceService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 设备列表
 * @Author: jeecg-boot
 * @Date:   2021-11-08
 * @Version: V1.0
 */
@Service
public class LwgyCheckEquipmentServiceImpl extends ServiceImpl<LwgyCheckEquipmentMapper, LwgyCheckEquipment> implements ILwgyCheckEquipmentService {

	@Autowired
	private LwgyCheckEquipmentMapper lwgyCheckEquipmentMapper;
	@Autowired
	private LwgyCheckRecordsMapper lwgyCheckRecordsMapper;
	@Autowired
	private ILwgyRepairMaintenanceService lwgyRepairMaintenanceService;

	@Override
	@Transactional
	public void saveMain(LwgyCheckEquipment lwgyCheckEquipment, List<LwgyCheckRecords> lwgyCheckRecordsList) {
		lwgyCheckEquipmentMapper.insert(lwgyCheckEquipment);
		if(lwgyCheckRecordsList!=null && lwgyCheckRecordsList.size()>0) {
			for(LwgyCheckRecords entity:lwgyCheckRecordsList) {
				//外键设置
				entity.setEquipmentId(lwgyCheckEquipment.getId());

				if(entity.getEquipmentIsNormal().equals("1")){//正常为0，异常为1

					String data = DateUtils.getTimestamp().toString();//时间戳
					//数据异常时添加到报修中
					LwgyRepairMaintenance lwgyRepairMaintenance = new LwgyRepairMaintenance();
					lwgyRepairMaintenance.setId(data);
					lwgyRepairMaintenance.setCampusId("校区暂无");
					lwgyRepairMaintenance.setBuildingId("楼宇暂无");
					lwgyRepairMaintenance.setUnitId("单元暂无");
					lwgyRepairMaintenance.setContact("自动报修");
					lwgyRepairMaintenance.setBcatalogid("1");//报修分类，设置默认
					lwgyRepairMaintenance.setRemark("设备名称："+lwgyCheckEquipment.getEquipmentName()+"-设备位置："+lwgyCheckEquipment.getEquipmentPlaceId()+"-异常描述"+entity.getAnnormalDescription());//异常描述写在报修备注里面
					lwgyRepairMaintenanceService.save(lwgyRepairMaintenance);
					//数据异常时，将提交状态修改为已报修
					entity.setAnnormalIsbx("1");
					entity.setRepairsId(data);//报修单号
					entity.setBxTime(DateUtils.now());

				}

				lwgyCheckRecordsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(LwgyCheckEquipment lwgyCheckEquipment,List<LwgyCheckRecords> lwgyCheckRecordsList) {
		lwgyCheckEquipmentMapper.updateById(lwgyCheckEquipment);
		
		//1.先删除子表数据
		lwgyCheckRecordsMapper.deleteByMainId(lwgyCheckEquipment.getId());
		
		//2.子表数据重新插入
		if(lwgyCheckRecordsList!=null && lwgyCheckRecordsList.size()>0) {
			for(LwgyCheckRecords entity:lwgyCheckRecordsList) {
				//外键设置
				entity.setEquipmentId(lwgyCheckEquipment.getId());
				lwgyCheckRecordsMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		lwgyCheckRecordsMapper.deleteByMainId(id);
		lwgyCheckEquipmentMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			lwgyCheckRecordsMapper.deleteByMainId(id.toString());
			lwgyCheckEquipmentMapper.deleteById(id);
		}
	}
	
}
